#include "my_stack.h"

int main() {
    pStack stack = make_stack();
    char tmp;
    int tmp_value,type = -1,used = 0;
    while(scanf("%c",&tmp)){
        //空格是常见的分隔符
        if(tmp==' '){
            //当还在输入运算符时，无视空格
            //当输入数字时，空格表示将当前结果入栈
            if(type==number&&used==0){
                pItem item = calloc(1,sizeof(Item));
                item->type = number;
                item->value = tmp_value;
                push(stack,item);
                tmp_value = 0;
                used = 1;
            }
            continue;
        }
        if(tmp=='_'||tmp=='\n') break;
        if('0'<=tmp && tmp<='9'){
            //先前输入的是符号
            if(type==character){
                tmp_value = 0;
            }
            type = number;
            used = 0;
            tmp_value = 10*tmp_value + (tmp-'0');
        }else{
            if(type==number&&used==0){
                //扫描得到的数字将入栈
                pItem item = calloc(1,sizeof(Item));
                item->type = number;
                item->value = tmp_value;
                push(stack,item);
                tmp_value = 0;
                used = 1;
            }
            type = character;
            /*
            pItem item = calloc(1,sizeof(Item));
            item->type = character;
            item->value = tmp;
            push(stack,item);
            */

            int op2 = peek(stack)->value;
            pop(stack);
            int op1 = peek(stack)->value;
            pop(stack);
            pItem item = calloc(1,sizeof(Item));
            item->type = number;
            switch (tmp) {
                case '+':{
                    item->value = op1 + op2;
                    break;
                }
                case '-':{
                    item->value = op1 - op2;
                    break;
                }
                case '*':{
                    item->value = op1 * op2;
                    break;
                }
                case '/':{
                    item->value = op1 / op2;
                    break;
                }
                default:{
                    printf("error!");
                    destroy_stack(stack);
                    return -1;
                }
            }
            push(stack,item);
        }
    }
    printStackTrace(stack);
    destroy_stack(stack);
    return 0;
}
